package com.yyfax.ph.common.widgets;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 用Glide在Adapter里面加载图片，如果图片宽高设置了wrap_content，则图片不展示，重写了onMeasure方法
 * http://www.jianshu.com/p/00edef7da311
 * Created by zhangxian on 2017/11/10.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class ResizableImageView extends ImageView {

	public ResizableImageView(Context context) {
		super(context);
	}

	public ResizableImageView(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
	}

	public ResizableImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		Drawable d = getDrawable();
		if(d!=null){
			int width = MeasureSpec.getSize(widthMeasureSpec);
			//高度根据使得图片的宽度充满屏幕计算而得
			int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
			setMeasuredDimension(width, height);
		}else{
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
	}
}
